// miniprogram/packageUser/pages/feedback/feedback.js
import {verifyInput} from "../../../utils/Util";
import {addFeedback} from "../../../service/User";
import {getStorageSync} from "../../../utils/Storage"
import Notify from "@vant/weapp/notify/notify";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        formData: {
            intro: "",
            contact: "",
            images: []
        },
        fileSavePath: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let openId = getStorageSync("userInfo.openId");
        this.setData({fileSavePath: "feedback/" + (openId ? openId + "/" : "")});
    },

    /*
    * input输入内容
    * */
    onChange: function (e) {
        let name = e.currentTarget.dataset.name;
        this.setData({["formData." + name]: e.detail})
    },

    /*
      * 图片接收函数
      * */
    onUploadFile: function (e) {
        this.data.formData.images = e.detail.result.map(function (item) {
            return item.id
        })
    },

    /*
    * 点击验证发布
    * */
    onSubmitBtn: async function () {
        const that = this;
        try {
            let intro = that.data.formData.intro;
            let loading = that.data.loading;
            if (!verifyInput(intro)) {
                Notify({message: "内容不能为空"});
                return false
            } else if (loading) {
                return false
            } else {
                await addFeedback(that.data.formData)
                that.setData({loading: true});
                Toast.success({
                    message: "提交成功",
                    mask: true,
                    onClose: () => {
                        wx.navigateBack()
                    }
                });
            }
        } catch (e) {
            that.setData({loading: false})
            Toast.fail("提交失败");
            console.error(e)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    onShareTimeline: function () {}
})